package com.cskaoyan.shopping.controller;


import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shopping")
public class DeleteCartController {

    @Autowired
    ICartService iCartService;

    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable long uid, @PathVariable long pid) {
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setItemId(pid);
        deleteCartItemRequest.setUserId(uid);
        DeleteCartItemResponse response = iCartService.deleteCartItem(deleteCartItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getResult());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/items/{uid}")
    public ResponseData deleteCheckedItem(@PathVariable long uid){
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);
        DeleteCheckedItemResposne response = iCartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getResult());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
    @PutMapping("/items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllItemRequest checkAllItemRequest){
        CheckAllItemResponse response = iCartService.checkAllCartItem(checkAllItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getResult());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
